package com.adingxiong.cft.auto;

import cn.hutool.setting.dialect.Props;
import cn.smallbun.screw.core.Configuration;
import cn.smallbun.screw.core.engine.EngineConfig;
import cn.smallbun.screw.core.engine.EngineFileType;
import cn.smallbun.screw.core.engine.EngineTemplateType;
import cn.smallbun.screw.core.execute.DocumentationExecute;
import cn.smallbun.screw.core.process.ProcessConfig;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;


import javax.sql.DataSource;
import java.util.Arrays;
import java.util.List;


/**
 * @ClassName DataSourceGenerate
 * @Description  自动生成数据库文档
 * @Author xiongchao
 * @Date 2020/9/9 13:54
 **/
public class DataSourceGenerate {
    private static Props props = null;

    static {
        props = new Props("genarater.properties");
    }

    /**
     * 初始化数据源配置
     */
    private static HikariConfig initDataSourceConfig() {

        HikariConfig hikariConfig = new HikariConfig();
        hikariConfig.setJdbcUrl(props.getStr("dataSource.url"));
        hikariConfig.setDriverClassName(props.getStr("dataSource.driverName"));
        hikariConfig.setUsername(props.getStr("dataSource.username"));
        hikariConfig.setPassword(props.getStr("dataSource.password"));
        return hikariConfig;
    }

    private static void autoGenerateWord(){
        //数据源
        HikariConfig hikariConfig = initDataSourceConfig();
        //设置可以获取tables remarks信息
        hikariConfig.addDataSourceProperty("useInformationSchema", "true");
        hikariConfig.setMinimumIdle(2);
        hikariConfig.setMaximumPoolSize(5);
        DataSource dataSource = new HikariDataSource(hikariConfig);

        String exportType = props.getStr("file.type","html");
        EngineFileType type = null;
        if(exportType.equalsIgnoreCase("md")){
            type= EngineFileType.MD;
        }else if (exportType.equalsIgnoreCase("word")){
            type = EngineFileType.WORD;
        }else {
            type = EngineFileType.HTML;
        }
        //生成配置
        EngineConfig engineConfig = EngineConfig.builder()
                //生成文件路径
                .fileOutputDir("./")
                //打开目录
                .openOutputDir(true)
                //生成模板实现
                .produceType(EngineTemplateType.freemarker)
                //文件类型
                .fileType(type)
                .build();

        //读取配置
        List<String> ignoreTableName = Arrays.asList(props.getStr("ignore.table").split(","));
        //忽略表前缀
        List<String> ignorePrefix =Arrays.asList(props.getStr("ignore.table.prefix").split(","));
        //忽略表后缀
        List<String> ignoreSuffix = Arrays.asList(props.getStr("ignore.table.suffix").split(","));
        ProcessConfig processConfig = ProcessConfig.builder()
                //忽略表名
                .ignoreTableName(ignoreTableName)
                //忽略表前缀
                .ignoreTablePrefix(ignorePrefix)
                //忽略表后缀
                .ignoreTableSuffix(ignoreSuffix).build();
        //配置
        Configuration config = Configuration.builder()
                //版本
                .version("1.0.0")
                //描述
                .description("数据库设计文档生成")
                //数据源
                .dataSource(dataSource)
                //生成配置
                .engineConfig(engineConfig)
                //生成配置
                .produceConfig(processConfig).build();
        //执行生成
        new DocumentationExecute(config).execute();
    }


    public static void main(String[] args) {
        autoGenerateWord();
    }
}
